import {
  defineContentConfig,
  defineCollection,
  defineCollectionSource,
  type CollectionSource,
  z,
} from "@nuxt/content";

const documentSource = defineCollectionSource({
  getKeys: async () => {
    return fetch("http://127.0.0.1:8080/api/content/document/pathAll")
      .then(async (res) => {
        const r = await res.json();
        return r.data;
      })
      .then((data) => {
        return data.map((key) => {
          return `${key.id}`;
        });
      });
  },
  getItem: async (id: string) => {
    return fetch(`http://127.0.0.1:8080/api/content/document/${id}`).then(
      async (res) => {
        const r = await res.json();
        console.log("获取到的文件r", r);
        return r.data.text;
      }
    );
  },
});

export default defineContentConfig({
  collections: {
    document: defineCollection({
      type: "page",
      source: documentSource,
      schema: z.object({
        title: z.string(),
        date: z.string(),
        type: z.string(),
        score: z.number(),
        url: z.string(),
        by: z.string(),
      }),
    }),
  },
});
